package com.elong.qadev.service.impl;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elong.qadev.dao.HttpCaseDaoI;
import com.elong.qadev.entity.DBLink;
import com.elong.qadev.entity.DBSql;
import com.elong.qadev.entity.Entities;
import com.elong.qadev.entity.Header;
import com.elong.qadev.entity.HttpCase;
import com.elong.qadev.entity.NameValuePair;
import com.elong.qadev.entity.RunAfter;
import com.elong.qadev.entity.RunBefore;
import com.elong.qadev.entity.UrlParam;
import com.elong.qadev.self.ParamSave;
import com.elong.qadev.self.RunCase;
import com.elong.qadev.self.Verify;
import com.elong.qadev.service.HttpCaseServiceI;

@Service("httpCaseService")
public class HttpCaseServiceImpl implements HttpCaseServiceI {
	Logger logger = Logger.getLogger(HttpCaseServiceImpl.class);
	@Autowired
	private HttpCaseDaoI httpCaseDao;

	@Override
	public ArrayList<Header> getHeaders(int caseId) {
		return httpCaseDao.getHeaders(caseId);
	}

	@Override
	public Entities getEntities(int caseId) {
		return httpCaseDao.getEntities(caseId);
	}

	@Override
	public ArrayList<NameValuePair> getNameValuePairs(int caseId) {
		return httpCaseDao.getNameValuePairs(caseId);
	}

	@Override
	public ArrayList<UrlParam> getUrlParams(int caseId) {
		return httpCaseDao.getUrlParams(caseId);
	}

	@Override
	public HttpCase getHttpCase(int caseId) {
		return httpCaseDao.getHttpCase(caseId);
	}

	@Override
	public ArrayList<RunBefore> getRunBefores(int caseId) {

		return httpCaseDao.getRunBefores(caseId);
	}

	@Override
	public ArrayList<RunAfter> getRunAfters(int caseId) {

		return httpCaseDao.getRunAfters(caseId);
	}

	@Override
	public boolean saveHeaders(ArrayList<Header> ah) {

		return httpCaseDao.saveHeaders(ah);
	}

	@Override
	public boolean saveEntities(Entities e) {

		return httpCaseDao.saveEntities(e);
	}

	@Override
	public boolean saveNameValuePairs(ArrayList<NameValuePair> an) {

		return httpCaseDao.saveNameValuePairs(an);
	}

	@Override
	public boolean saveUrlParams(ArrayList<UrlParam> au) {

		return httpCaseDao.saveUrlParams(au);
	}

	@Override
	public boolean saveHttpCase(HttpCase hc) {

		return httpCaseDao.saveHttpCase(hc);
	}

	@Override
	public boolean saveRunBefores(ArrayList<RunBefore> lb) {

		return httpCaseDao.saveRunBefores(lb);
	}

	@Override
	public boolean saveRunAfters(ArrayList<RunAfter> lr) {

		return httpCaseDao.saveRunAfters(lr);
	}

	@Override
	public boolean saveResponseAndResult(int caseId, boolean isExpected,
			String response) {
		logger.info("开始执行存储...");
		return httpCaseDao.saveResponseAndResult(caseId, isExpected, response);
	}

	@Override
	public boolean deleteAllCaseInfo(int caseId) {
		if (httpCaseDao.deleteHttpCase(caseId)
				&& httpCaseDao.deleteEntities(caseId)
				&& httpCaseDao.deleteHeaders(caseId)
				&& httpCaseDao.deleteNameValuePairs(caseId)
				&& httpCaseDao.deleteRunAfters(caseId)
				&& httpCaseDao.deleteUrlParams(caseId)
				&& httpCaseDao.deleteRunBefores(caseId)) {
			return true;
		}
		return false;
	}

	@Override
	public Map<String, ArrayList<String>> getBeforeClassInfo() {
		Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
		ArrayList<String> methodListRunCase = new ArrayList<String>();
		ArrayList<String> methodListParamSave = new ArrayList<String>();
		ArrayList<String> methodListParamGen = new ArrayList<String>();
		ArrayList<String> methodListParamDb = new ArrayList<String>();
		try {
			Class<?> before = Class
					.forName("com.elong.qadev.self.BeforeMethod");
			logger.info("获取类名是：" + before.getName());
			Method[] methods = before.getDeclaredMethods();
			logger.info("获取的方法集合是：" + Arrays.toString(methods));
			String des = "";
			String type = "";
			for (int i = 0; i < methods.length; i++) {
				String args = "";
				int tempi = 0;
				logger.info("获取到的方法名称是：" + methods[i].getName());
				Class<?>[] argsTypes = methods[i].getParameterTypes();

				if (argsTypes.length <= 0) {
					args += "此方法无需输入参数";
				}
				for (Class<?> param : argsTypes) {
					// logger.info("开始执行参数获取循环："+param.getName());
					String[] argsLast = param.getName().split("\\.");
					// logger.info("获取的数组是："+Arrays.toString(argsLast));
					if (tempi == 0)
						args += argsLast[argsLast.length - 1];
					else
						args += "，" + argsLast[argsLast.length - 1];
					tempi++;
				}

				logger.info("方法参数是：" + args);
				if (methods[i].getAnnotation(ParamSave.class) != null) {
					des = methods[i].getAnnotation(ParamSave.class)
							.description();
					type = methods[i].getAnnotation(ParamSave.class).type();
					logger.info("获取到的注释描述是：" + des);
					logger.info("获取到的注释类型是：" + type);
					if (type.equalsIgnoreCase("PARAM_SAVE"))
						methodListParamSave.add(methods[i].getName() + "#,#"
								+ des + "#,#" + args);
					if (type.equalsIgnoreCase("PARAM_GEN"))
						methodListParamGen.add(methods[i].getName() + "#,#"
								+ des + "#,#" + args);
					if (type.equalsIgnoreCase("PARAM_DB"))
						methodListParamDb.add(methods[i].getName() + "#,#"
								+ des + "#,#" + args);
				} else if (methods[i].getAnnotation(RunCase.class) != null) {
					des = methods[i].getAnnotation(RunCase.class).description();
					logger.info("获取到的注释描述是：" + des);
					logger.info("获取到的注释类型是：" + type);
					type = methods[i].getAnnotation(RunCase.class).type();
					if (type.equalsIgnoreCase("RUN_CASE"))
						methodListRunCase.add(methods[i].getName() + "#,#"
								+ des + "#,#" + args);
				} else {
					continue;
				}
			}

			map.put("参数设置", methodListParamSave);
			map.put("参数DB提取", methodListParamDb);
			map.put("参数生成", methodListParamGen);
			map.put("用例运行", methodListRunCase);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public Map<String, ArrayList<String>> getAfterClassInfo() {
		Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
		ArrayList<String> methodListParamSave = new ArrayList<String>();

		ArrayList<String> methodListVerify_String = new ArrayList<String>();
		ArrayList<String> methodListVerify_Obj = new ArrayList<String>();
		ArrayList<String> methodListVerify_Comp = new ArrayList<String>();
		ArrayList<String> methodListVerify_Check = new ArrayList<String>();
		ArrayList<String> methodListVerify_Db = new ArrayList<String>();

		try {
			Class<?> after = Class.forName("com.elong.qadev.self.AfterMethod");
			logger.info("获取类名是：" + after.getName());
			Method[] methods = after.getDeclaredMethods();
			logger.info("获取的方法集合是：" + Arrays.toString(methods));
			String des = "";
			String type = "";
			for (int i = 0; i < methods.length; i++) {
				String args = "";
				int tempi = 0;
				logger.info("获取到的方法名称是：" + methods[i].getName());
				Class<?>[] argsTypes = methods[i].getParameterTypes();

				if (argsTypes.length <= 0) {
					args += "此方法无需输入参数";
					continue;
				}
				for (Class<?> param : argsTypes) {
					// logger.info("开始执行参数获取循环："+param.getName());
					String[] argsLast = param.getName().split("\\.");
					// logger.info("获取的数组是："+Arrays.toString(argsLast));
					if (tempi == 0)
						args += argsLast[argsLast.length - 1];
					else
						args += "，" + argsLast[argsLast.length - 1];
					tempi++;
				}
				if (methods[i].getAnnotation(ParamSave.class) != null) {
					des = methods[i].getAnnotation(ParamSave.class)
							.description();
					type = methods[i].getAnnotation(ParamSave.class).type();
					logger.info("获取到的注释描述是：" + des);
					logger.info("获取到的注释类型是：" + type);
					if (type.equalsIgnoreCase("PARAM_JSON"))
						methodListParamSave.add(methods[i].getName() + "#,#"
								+ des + "#,#" + args);
				} else if (methods[i].getAnnotation(Verify.class) != null) {
					des = methods[i].getAnnotation(Verify.class).description();
					type = methods[i].getAnnotation(Verify.class).type();
					logger.info("获取到的注释描述是：" + des);
					logger.info("获取到的注释类型是：" + type);
					if (type.equalsIgnoreCase("VERIFY_JSON_STRING"))
						methodListVerify_String.add(methods[i].getName()
								+ "#,#" + des + "#,#" + args);
					if (type.equalsIgnoreCase("VERIFY_JSON_OBJ"))
						methodListVerify_Obj.add(methods[i].getName()
								+ "#,#" + des + "#,#" + args);
					if (type.equalsIgnoreCase("VERIFY_JSON_COMP"))
						methodListVerify_Comp.add(methods[i].getName()
								+ "#,#" + des + "#,#" + args);
					if (type.equalsIgnoreCase("VERIFY_JSON_CHECK"))
						methodListVerify_Check.add(methods[i].getName()
								+ "#,#" + des + "#,#" + args);
					if (type.equalsIgnoreCase("VERIFY_JSON_DB"))
						methodListVerify_Db.add(methods[i].getName()
								+ "#,#" + des + "#,#" + args);
				} else {
					continue;
				}
			}
			map.put("JSON参数保存", methodListParamSave);
			map.put("JSON字符校验", methodListVerify_String);
			map.put("JSON对象校验", methodListVerify_Obj);
			map.put("JSON数据比较", methodListVerify_Comp);
			map.put("JSON数据检查", methodListVerify_Check);
			map.put("JSON-DB校验", methodListVerify_Db);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return map;
	}

	public static void main(String[] args) {
		HttpCaseServiceImpl h = new HttpCaseServiceImpl();
		Map<String, ArrayList<String>> map = h.getBeforeClassInfo();
		System.out.println("----------------------");
		for (String key : map.keySet()) {
			for (String value : map.get(key)) {
				System.out.println(value);
			}
		}
		System.out.println("----------------------");
		h.logger.info("------------------------------------------");
		h.getAfterClassInfo();
	}

	@Override
	public DBSql getDBSqlById(int sqlId) {
		// TODO Auto-generated method stub
		return httpCaseDao.getDBSqlById(sqlId);
	}

	@Override
	public DBLink getDBLinkById(int sqlId) {
		// TODO Auto-generated method stub
		return httpCaseDao.getDBLinkById(sqlId);
	}

	@Override
	public ArrayList<DBLink> getDBLinks() {
		// TODO Auto-generated method stub
		return httpCaseDao.getDBLinks();
	}

	@Override
	public ArrayList<DBSql> getDBSqls() {
		// TODO Auto-generated method stub
		return httpCaseDao.getDBSqls();
	}

	@Override
	public boolean saveDBLink(DBLink dbLink) {
		// TODO Auto-generated method stub
		return httpCaseDao.saveDBLink(dbLink);
	}

	@Override
	public boolean saveDBSql(DBSql dbSql) {
		// TODO Auto-generated method stub
		return httpCaseDao.saveDBSql(dbSql);
	}

	@Override
	public boolean deleteDBLink(int id) {
		// TODO Auto-generated method stub
		return httpCaseDao.deleteDBLink(id);
	}

	@Override
	public boolean deleteDBSql(int id) {
		// TODO Auto-generated method stub
		return httpCaseDao.deleteDBSql(id);
	}

}
